package xyz.xiangyushui.dao.salesPlan;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import xyz.xiangyushui.dao.Dao;
import xyz.xiangyushui.javaBean.salesPlan.DailyPlan;
import xyz.xiangyushui.javaBean.salesPlan.DailyPlanCommodity;

public class SalesPlanDao extends Dao {

	// planId：date+"_"+storeId

	public void addDailyPlan(DailyPlan plan) {
		db.executeUpdate("insert into dailyPlan values(?,?)", new Object[] {
				plan.getDate() + "_" + plan.getStoreId(), 0 });
		List<DailyPlanCommodity> commdities = plan.getCommodity();
		for (DailyPlanCommodity c : commdities) {
			db.executeUpdate(
					"insert into dailyPlanCommodity(planId,commodityIdName,num,price) values(?,?,?,?) ",
					new Object[] { plan.getDate() + "_" + plan.getStoreId(),
							c.getCommodityIdName(), c.getNum(), c.getPrice() });
		}

	}
	
	
	public List<String> getSalesPlanIdReadyToBeApproved(){
		return getPlansId(0);
	}
	
	public List<String> getSalesPlanRejected(int storeId){
		return getPlansId(1,storeId);
	}
	
	
	private List<String> getPlansId(int state){
		List<String> plans = new LinkedList<>();
		List<Object> list = db.excuteQuery(
				"select planId from dailyPlan where state=? ",
				new Object[] { state });
		if (!list.isEmpty()) {
			for (Object o : list) {
				@SuppressWarnings("unchecked")
				Map<String, Object> m = (Map<String, Object>) o;
				String planId = (String) m.get("planId");
				plans.add(planId);
			}
		}
		return plans;
	}
	
	private List<String> getPlansId(int state,int storeId){
		List<String> plans = new LinkedList<>();
		List<Object> list = db.excuteQuery(
				"select planId from dailyPlan where state=? ",
				new Object[] { state });
		if (!list.isEmpty()) {
			for (Object o : list) {
				@SuppressWarnings("unchecked")
				Map<String, Object> m = (Map<String, Object>) o;
				String planId = (String) m.get("planId");
				String[] s=planId.split("_");
				if(Integer.parseInt(s[1])==(storeId)){
					plans.add(planId);
				}
			}
		}
		return plans;
	}
	
	

	public List<DailyPlan> getApprovablePlans() {
		List<DailyPlan> plans = new LinkedList<>();
		List<Object> list = db.excuteQuery(
				"select planId from dailyPlan where state=? ",
				new Object[] { 0 });
		if (!list.isEmpty()) {
			for (Object o : list) {
				@SuppressWarnings("unchecked")
				Map<String, Object> m = (Map<String, Object>) o;
				String planId = (String) m.get("planId");
				DailyPlan dp = new DailyPlan();
				String[] date_storeId=planId.split("_");
				dp.setDate(Long.parseLong(date_storeId[0]));
				dp.setStoreId(Integer.parseInt(date_storeId[1]));
				List<DailyPlanCommodity> commodities=getCommodity(planId);
				dp.setCommodity(commodities);
				plans.add(dp);
			}
		}
		return plans;
	}

	public DailyPlan getDailyPlan(String planId,int state){
		DailyPlan dp=null;
		List<Object> list = db.excuteQuery(
				"select planId from dailyPlan where planId=? and state=? ",
				new Object[] {planId,state});
		if(!list.isEmpty()){
			dp=new DailyPlan();
			String[] date_storeId=planId.split("_");
			dp.setDate(Long.parseLong(date_storeId[0]));
			dp.setStoreId(Integer.parseInt(date_storeId[1]));
			dp.setCommodity(getCommodity(planId));
			dp.setState(state);
		}
		return dp;
	}
	
	private List<DailyPlanCommodity> getCommodity(String planId){
		List<DailyPlanCommodity> commodities=new LinkedList<>();
		List<Object> list=db.excuteQuery("select id,commodityIdName,num,price from dailyPlanCommodity where planId=? ", new Object[]{planId});
		if(!list.isEmpty()){
			for(Object o:list){
				@SuppressWarnings("unchecked")
				Map<String,Object> m=(Map<String, Object>) o;
				DailyPlanCommodity dpc=new DailyPlanCommodity();
				dpc.setCommodityIdName((String) m.get("commodityIdName"));
				dpc.setNum((int) m.get("num"));
				dpc.setPrice((double) m.get("price"));
				commodities.add(dpc);
			}
		}
		return commodities;
	}

	public void update(DailyPlan plan) {
		db.executeUpdate("delete from dailyPlan where planId=?",
				new Object[] { plan.getDate() + "_" + plan.getStoreId()});
		db.executeUpdate("delete  from dailyPlanCommodity where planId=?",
				new Object[] { plan.getDate() + "_" + plan.getStoreId()});
		addDailyPlan(plan);
	}

	public boolean exists(long date, int storeId) {
		List<Object> list = db.excuteQuery(
				"select * from dailyPlan where planId=?", new Object[] { date
						+ "_" + storeId });
		return !list.isEmpty();
	}
	
	public boolean exists(String planId) {
		List<Object> list = db.excuteQuery(
				"select * from dailyPlan where planId=?", new Object[] { planId});
		return !list.isEmpty();
	}

	public void reject(String planId) {
		changeState(planId, 1);
	}

	public void approve(String planId) {
		changeState(planId, 2);
		List<DailyPlanCommodity> commdities = getCommodity(planId);
		for (DailyPlanCommodity c : commdities) {
			System.out.println("approve:"+c.getCommodityIdName());
			db.executeUpdate(
					"insert into commodityReservable(planId,commodityIdName,stock,price) values(?,?,?,?) ",
					new Object[] { planId,
							c.getCommodityIdName(), c.getNum(), c.getPrice() });
		}
		
	}
	

	private void changeState(String planId, int state) {
		db.executeUpdate("update dailyPlan set state=? where planId=? ",
				new Object[] { state, planId });
	}
}
